package com.ysd.lis.controller.sys;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import com.ysd.common.Result;
import com.ysd.lis.controller.pub.BaseController;
import com.ysd.lis.service.DictProvinceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Api(value = "公共资源查询")
@RestController
@RequestMapping("/api/sys/ckEditor")
public class CkEditorController extends BaseController {

    private static final int MAX_POST_SIZE = 20 * 1024 * 1024;
    private static final String ueditPicturePath = "D:\\lisFile\\uedit\\picture";

    @ApiOperation("上传图片")
    @PostMapping("/uploadUeditPicture")
    public Result uploadUeditPicture(@RequestParam(value = "file") MultipartFile file, HttpServletRequest request) throws IOException {
        String name = file.getOriginalFilename();
        if (file == null) {
            return Result.fail("文件不能为空");
        }
        // 文件限制10M
        long size = file.getSize();
        if (size > MAX_POST_SIZE) {
            return Result.fail("length exceeds limit of 10M");
        }
        String folder = ueditPicturePath + "\\" + DateUtil.format(new Date(), "yyyyMM") + File.separator;
        if (!FileUtil.exist(folder)) {
            FileUtil.mkdir(folder);
        }
        String date = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddhhmmssSSS"));
        String fileName = date + file.getOriginalFilename();
        String path = folder + fileName;
        File files = new File(path);
        if (FileUtil.exist(files)) {
            return Result.fail("文件已存在");
        }
        File file1 = FileUtil.writeBytes(file.getBytes(), path);
        if (file1.length() < 0) {
            return Result.fail("文件上传失败");
        }
        Map returnMap = new HashMap();
        String returnPath = "/api/sys/ckEditor/viewUeditPicture?path=" + URLEncoder.encode(path, "utf-8");
        returnMap.put("path", returnPath);
        return Result.succ(1, "上传成功！", returnMap);
    }


    @GetMapping("/viewUeditPicture")
    public void viewUeditPicture(@RequestParam String path, HttpServletResponse response) {
        try {
            path = URLDecoder.decode(path, "utf-8");
            File file = new File(path);
            FileInputStream fileInputStream = new FileInputStream(file);
            long size = file.length();
            byte[] temp = new byte[(int) size];
            fileInputStream.read(temp, 0, (int) size);
            fileInputStream.close();
            byte[] data = temp;
            response.setContentType("image/png");
            OutputStream os = response.getOutputStream();
            os.write(data);
            os.flush();
            os.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
